Skip to content

add screenshots to components gallery #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 12, 2016
Merged

add screenshots to components gallery #125

merged 7 commits into from
Dec 12, 2016

Conversation

sebastienbarre
Copy link
Contributor

Howdy.
Love the components gallery in Tachyons, but the list is growing so fast I'm starting to get lost :)

This PR adds a new build script that creates screenshots for each component.

screen shot 2016-12-09

  • src/components-build.s was updated to return a promise (since it is asynchronous), so that another script can be executed after it has fully completed.
  • src/components-screenshots-build.js takes care of creating the screenshots and return a promise as well (since it is asynchronous).
  • build.js was updated to handle the promises returned by the above.

Screenshots are created automatically. A bare-bone Express static HTML server is started and used to load each component page one by one for capture. The component template was updated to wrap each component inside a <div data-name="component-container"> so that the corresponding area in the page can be captured.

This works for the vast majority of them. Some components needed a bit of help, and for that the front matter was leveraged to instruct the script on how best to extract and format the screenshot. For example, most of the buttons examples needed this small change:

{{{
  "bodyClass" : "bg-white pt5",
  "screenshot" : {
    "selector" : "[data-name=\"screenshot\"]",
    "background-position" : "left center"
  }
}}}
  • a <div data-name="screenshot"> has been placed around a specific subset of buttons -- without it, the page is so large that one can barely see the difference between each buttons.
  • since this example page is left aligned, the background-position attribute is used above to override the default center center in the template.

Another option:

  "screenshot" : {
    "background-size" : "contain"
  }

to have Tachyons use contain instead of cover for the screenshot.

This PR only contains the scripts and updates to some of the components and templates. I can push the generated pages as well (i.e. the whole components dir and screenshots).

TODO:

  • I used floats to create that screenshot gallery, but I'm sure you guys can find a better layout.
  • the page is a lot heavier now, and will take some time to load. The PNG should be resized to 512px wide or less, and optimized (using imagemin for example). But that was a fair amount of work already, let's see if you guys like this screenshot feature first.

Here is the whole page:

screen shot 2016-12-09 at 22 33 43-fullpage

@sebastienbarre
Copy link
Contributor Author

Build process cleaned up and updated, looks like that now:

const componentsBuildIndex = require('./src/components-build-index');
const componentsBuildPages = require('./src/components-build-pages');
const componentsBuildScreenshots = require('./src/components-build-screenshots');

const globPattern = 'src/components/cards/*.html';
componentsBuildIndex(globPattern)
.then(componentsForNavPath => componentsBuildPages(componentsForNavPath))
.then(componentsForNavPath => componentsBuildScreenshots(componentsForNavPath))
.then(() => {
  console.log('All done');
})
.catch((err) => {
  console.log(err);
});

You may need Node 6 as I used ES2015 "modern" syntax :)

The first step, which builds the index, now create an intermediate file tmp/componentsForNav.json and returns that path to be used by other (asynchronous) steps, so that the code that finds the component name, paths, screenshot name, path, etc is not duplicated. The front matter is in there too. For example:

{
  "cards": [
    {
      "name": "Album Centered",
      "title": "Cards Album Centered",
      "src": "src/components/cards/album-centered.html",
      "path": "components/cards/album-centered/index.html",
      "href": "/components/cards/album-centered/index.html",
      "screenshot": {
        "path": "components/cards/album-centered/screenshot.png",
        "href": "/components/cards/album-centered/screenshot.png"
      },
      "frontMatter": {
        "bodyClass": "bg-white pt5"
      }
    },

@sebastienbarre
Copy link
Contributor Author

sebastienbarre commented Dec 10, 2016

Add component signature, a hash of the Tachyons version and component contents, so it can be used to bust the browser cache for screenshots. If the server delivering these pages is setup correctly, a user returning to the components page should not re-download the screenshots, but use the ones in the cache instead, UNLESS the signature has changed, in which case they will be re-downloaded. Win-win.

@mrmrs @johnotander I assume you are using Github Pages? We will have to test, it's unclear how caching is setup, though it might be something a bit too short like 10 minutes :( See javascript - Determining a page is outdated on github pages, How to solve caching problem with Github Pages, Caching assets in Github pages (github.io)

{
  "banners": [
    {
      "name": "Basic",
      "title": "Banners Basic",
      "src": "src/components/banners/basic.html",
      "path": "components/banners/basic/index.html",
      "href": "/components/banners/basic/index.html",
      "screenshot": {
        "path": "components/banners/basic/screenshot.png",
        "href": "/components/banners/basic/screenshot.png?version=71b1b1c5aac3d21f986dd404d9f0f70f"
      },
      "signature": "71b1b1c5aac3d21f986dd404d9f0f70f",
      "frontMatter": {
        "bodyClass": "bg-white pt5"
      }
    },

@sebastienbarre
Copy link
Contributor Author

Script being run (only on the banners category):

1__bash_and___development_src_design_tachyons-css_github_io_src_components-build-index_js_ _tachyons-css_github_io

@sebastienbarre
Copy link
Contributor Author

sebastienbarre commented Dec 11, 2016

Phew.

  • build scripts were updated again, many configuration options can be overriden now (see src/components-build-defaults).
const componentsBuildIndex = require('./src/components-build-index');
const componentsBuildPages = require('./src/components-build-pages');
const componentsBuildScreenshots = require('./src/components-build-screenshots');

// See components-build-defaults for list of options
const options = {
  componentsGlobPattern: 'src/components/banners/*.html',
  componentsBuildPages: true, // false to skip
  componentsBuildScreenshots: true, // false to skip
};

componentsBuildIndex(options)
.then(() => componentsBuildPages(options))
.then(() => componentsBuildScreenshots(options))
.then(() => {
  console.log('All done');
})
  • images are shrunk and saved as JPEG.
Done with components screenshots! (20.87 MB => 3.58 MB)

There you go, 21MB to 3.6MB already, and the optimizer is not even there.

There is no algorithm that can perfectly fit the screenshot in the gallery layout, but this can be tweaked per component. I reviewed 150 of them (again), and nudged them accordingly. In the frontmatter, one can play with any one of these to change the defaults:

{{{
  "bodyClass" : "bg-white",
  "screenshot" : {
    "autocrop": false, 
    "background-position" : "left center",
    "selector" : "[data-name=\"screenshot\"]",
    "background-size" : "contain"
  }
}}}

@sebastienbarre
Copy link
Contributor Author

Scripts now show compression gain (from raw ONG capture to final, resized JPEG; no optimization for now)

1__bash_and_add_screenshots_to_components_gallery_by_sebastienbarre_ pull_request__125 _tachyons-css_tachyons-css_github_io

@sebastienbarre
Copy link
Contributor Author

sebastienbarre commented Dec 11, 2016

Alright folks,

  • The original 20.9MB of PNG screenshots capture for the components gallery are now resized and converted to JPEG, which decreased the load to 4.6MB.
  • I then added an extra optimization layer using imagemin which brought this whole thing down to 1.72MB.
  • I added lazy loading, so you are looking at only a few requests when you land on that page, and the rest is streamed as people scroll down.

I believe this should do the trick until you decide the gallery needs to be fragmented (I'm partial to a single page), but I got to work on other projects :) Hit me up though.

The compression quality and screenshot size can be tweaked in the future (check src/components-build-defaults.js).

@mrmrs @johnotander what do you think? You guys will be the ones adding components, I cleaned up build.js but let me know if this gets in the way of your development workflow. You may need a recent version of node.js.

I haven't committed the new, generated gallery, just the source files. Give it a shot. It slower though, unsurprisingly, it took about 5 mins to re-create it on a 2016 MBP.

With that being said it should be easy to create a "Featured" gallery, or an index to category pages, with only one "featured" screenshot per category in that index. I would slap: "featured": true in the front matter. As far as showing the most recent components, you could add a date there too, but git can tell us when a file was last added, git log --diff-filter=A -- foo.js, and there might be a NPM package to access that programmatically from a script (and that's nodegit).

@johno
Copy link
Member

johno commented Dec 11, 2016

Hey @sebastienbarre, thank you so much for your contribution. This is amazing!

Just ran the build and did some testing locally. Works perfectly. The lazy loading of the images keeps the main component page pretty lightweight which is way rad. Everything looks good to me 👍 but I'll wait for @mrmrs to have a look to see if he has any thoughts or wants to tweak things.

I appreciate the time you took adding feedback to the build process, too. That's an aspect we've been needing to improve for a very long time : ).

@mrmrs
Copy link
Member

mrmrs commented Dec 12, 2016

@sebastienbarre you are my hero. This is all really rad. Great example of how to put together a solid PR. Appreciate all of your hard work here. Excited to launch this.

@mrmrs mrmrs merged commit a83bf8f into tachyons-css:master Dec 12, 2016
@sebastienbarre
Copy link
Contributor Author

Thanks folks, glad I could help.

@sebastienbarre sebastienbarre deleted the components_screenshots branch December 12, 2016 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants